package com.yw.android.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yw.android.common.utils.KVUtils;
import com.yw.android.common.utils.LOG;
import com.yw.android.common.utils.TextUtils;
import com.yw.android.sys.common.R;

/**
 * Created by yuanwei1 on 2017/3/9.
 */

public class WebViewActivity extends Activity {
    public final static String KEY_URL = "url";
    public static final String KEY_TITLE = "titleRes";
    private WebView mWebView;

    public static Intent getIntent(Context context, String title, String url) {
        try {
            Intent i = new Intent(context, WebViewActivity.class);
            if (!TextUtils.empty(title))
                KVUtils.put(i, KEY_TITLE, title);
            KVUtils.put(i, KEY_URL, url);
            return i;
        } catch (Throwable t) {
            LOG.e("[url:" + url + "] create intent failed(Throwable)", t);
        }
        return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assist);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void initView() {
        setTitle(KVUtils.get(getIntent(), KEY_TITLE, ""));
        mWebView = (WebView) findViewById(R.id.webview);

        //声明WebSettings子类
        WebSettings webSettings = mWebView.getSettings();
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LOG.d("shouldOverrideUrlLoading url : " + url);
                if (TextUtils.startsWith(url, "https://pet-chain.baidu.com/data/user/sign?")) {
                    CookieManager cookieManager = CookieManager.getInstance();
                    String CookieStr = cookieManager.getCookie(url);

                    Intent mIntent = new Intent();
                    mIntent.putExtra("cookie", CookieStr);
                    WebViewActivity.this.setResult(Activity.RESULT_OK, mIntent);
                    WebViewActivity.this.finish();
                }
                return false;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient());
        loadWebFile(KVUtils.get(getIntent(), KEY_URL, ""));
    }

    private void loadWebFile(String url) {
        if (TextUtils.empty(url)) {
            LOG.e("url is empty.");
        } else {
            LOG.d("url : " + url);
            mWebView.loadUrl(url);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
